<template>
	<el-dialog :title="!dataForm.id ? '新增' : '修改'" v-if="isAuth(['ROOT', 'DEPT:INSERT', 'DEPT:UPDATE'])" :close-on-click-modal="false" v-model="visible" width="420px">
		<el-form :model="dataForm" ref="dataForm" :rules="dataRule" label-width="100px">
			<el-form-item label="部门名称:" prop="name"><el-input v-model="dataForm.name" size="medium" style="width:100%" clearable /></el-form-item>
			<el-form-item label="部门管理员:" prop="realName">
        <el-select v-model="dataForm.managerId" placeholder="管理员" style="width: 240px">
          <el-option
              v-for="item in employee"
              :key="item.id"
              :label="item.realName"
              :value="item.id"
              :disabled="item.disabled"
          />
        </el-select>
      </el-form-item>
			<el-form-item label="上级部门:" prop="parentName">
        <div class="m-4">
          <el-cascader
              v-model="dataForm.parentId"
              :options="typeTree"
              :props="{checkStrictly :true ,label:'name',value:'id',emitPath:false}"
              clearable />
        </div>
      </el-form-item>
			<el-form-item label="部门机构:" prop="tenantId"><el-input v-model="dataForm.desc" style="width:100%" size="medium" maxlength="20" clearable /></el-form-item>
			<el-form-item label="部门状态:" prop="state">
        <el-radio-group v-model="dataForm.state">
          <el-radio value="0" size="large">正常</el-radio>
          <el-radio value="1" size="large">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button size="medium" @click="visible = false">取消</el-button>
				<el-button type="primary" size="medium" @click="dataFormSubmit">确定</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script>
export default {
	data: function() {
		return {
			visible: false,
      typeTree:[],
      employee:[],
			dataForm: {
				id: null,
				name: null,
        state:0,
        realName: null,
        managerId:null,
        parentName: null,
        parentId: null,
        tenantId: null
			},
			dataRule: {
				name: [{ required: true, pattern: '^[a-zA-Z0-9\u4e00-\u9fa5]{2,10}$', message: '部门名称格式错误' }],
				tel: [
					{
						required: false,
						pattern: '^1\\d{10}$|^(0\\d{2,3}\-){0,1}[1-9]\\d{6,7}$',
						message: '电话格式错误'
					}
				],
				email: [
					{
						required: false,
						pattern: '^([a-zA-Z]|[0-9])(\\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$',
						message: '邮箱格式错误'
					}
				]
			}
		};
	},

	methods: {
    getEmployeeName(){
      let that = this;
      that.get('/admin/manager/department/employeeName',{},function(resp) {
        console.log(111)
        console.log(resp)
        that.employee = resp
      })
    },
    getTypeTree(){
      let that = this;
      that.get('/admin/manager/department/typeTree',{},function(resp) {
        that.typeTree = resp
      })
    },
		init: function(id) {
			let that = this;
			that.dataForm.id = id || 0;
			that.visible = true;
			that.$nextTick(() => {
				that.$refs['dataForm'].resetFields();
				if (id) {
					that.get('admin/manager/department/'+id, function(resp) {
            console.log(resp)
						that.dataForm.name = resp.data.name;
						that.dataForm.state = resp.data.state;
						// that.dataForm.managerId = resp.data.managerId;
					});
				}
			});
		},
		dataFormSubmit: function() {
			let that = this;
			this.$refs['dataForm'].validate(valid => {
				if (valid) {
          // !that.dataForm.id动态构建请求，添加或修改
          // console.log(that.dataForm.id)
          console.log(that.dataForm)
					that.post(`admin/manager/department/${!that.dataForm.id ? 'insert' : 'update'}`, that.dataForm, function(resp) {
            console.log(resp)
						if (resp.success) {
							that.$message({
								message: '操作成功',
								type: 'success',
								duration: 1200
							});
							that.visible = false;
							that.$emit('refreshDataList');
						} else {
							that.$message({
								message: '操作失败',
								type: 'error',
								duration: 1200
							});
						}
					});
				}
			});
		}
	},
  mounted: function() {
    this.getTypeTree();
    this.getEmployeeName();
  }
};
</script>

<style lang="less" scoped="scoped"></style>
